<?php
/**
* @file $Id: MassSchedule.php 528 2007-05-21 19:55:02Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if(!$_REQUEST['modfunc'] && $_REQUEST['search_modfunc']!='list')
	unset($_SESSION['MassSchedule.php']);

if($_REQUEST['modfunc']=='save')
{
	if(count($_REQUEST['course_period_id']))
	{
		$start_date = $_REQUEST['day'].'-'.$_REQUEST['month'].'-'.$_REQUEST['year'];
		if(!VerifyDate($start_date))
			BackPrompt(_('The date you entered is not valid'));
		foreach($_REQUEST['course_period_id'] as $i=>$course_period_id)
		{
			$course_mp = DBGet(DBQuery("SELECT MARKING_PERIOD_ID FROM COURSE_PERIODS WHERE COURSE_PERIOD_ID='".$course_period_id."'"));
			$course_mp = $course_mp[1]['MARKING_PERIOD_ID'];
			$course_mp_table = GetMPTable(GetMP($course_mp,'TABLE'));
	
			if($_REQUEST['marking_period_id']=='!')
				$schedule_mp = $course_mp;
			elseif($course_mp_table!='FY' && $course_mp!=$_REQUEST['marking_period_id'] && strpos(GetChildrenMP($course_mp_table,$course_mp),"'".$_REQUEST['marking_period_id']."'")===false)
				BackPrompt(_("You cannot schedule a student into that course during the marking period that you chose.  This course meets on ").GetMP($course_mp).'.');
			else
				$schedule_mp = $_REQUEST['marking_period_id'];

			$mp_table = GetMPTable(GetMP($schedule_mp,'TABLE'));
		
			// THIS SHOULD USE GetStuList() so that it returns only relevant results -- this would be much faster on large databases
			$current_RET = DBGet(DBQuery("SELECT STUDENT_ID FROM SCHEDULE WHERE COURSE_PERIOD_ID='".$course_period_id."' AND SYEAR='".UserSyear()."' AND (('".$start_date."' BETWEEN START_DATE AND END_DATE OR END_DATE IS NULL) AND '".$start_date."'>=START_DATE)"),array(),array('STUDENT_ID'));
			foreach($_REQUEST['student'] as $student_id=>$yes)
			{
				if(!$current_RET[$student_id])
				{
					$sql = "INSERT INTO SCHEDULE (ID,SYEAR,SCHOOL_ID,STUDENT_ID,COURSE_ID,COURSE_WEIGHT,COURSE_PERIOD_ID,MP,MARKING_PERIOD_ID,START_DATE)
								values(".db_seq_nextval('SCHEDULE_SEQ').",'".UserSyear()."','".UserSchool()."','".$student_id."','".$_REQUEST['course_id'][$i]."','".$_REQUEST['course_weight'][$i]."','".$course_period_id."','".$mp_table."','".$schedule_mp."','".$start_date."')";
					DBQuery($sql);
				}
			}
			DBQuery("UPDATE COURSE_PERIODS SET FILLED_SEATS=FILLED_SEATS+'".count($_REQUEST['student'])."' WHERE COURSE_PERIOD_ID='".$course_period_id."'");
		}
		unset($_REQUEST['modfunc']);
		unset($_SESSION['MassSchedule.php']);
		unset($_REQUEST['course_period_id']);
		unset($_SESSION['_REQUEST_vars']['course_period_id']);
		unset($_SESSION['_REQUEST_vars']['modfunc']);
		if(count($_REQUEST['course_period_id'])==1)
			$note = _("That course has been added to the selected students' schedules.");
		else
			$note = _("Those courses have been added to the selected students' schedules.");		
	}
	else
		BackPrompt(_('You must choose a Course'));
}


if($_REQUEST['modfunc']!='choose_course')
{
	unset($_SESSION['MassSchedule.php']);
	DrawHeader(ProgramTitle());
	if($note)
		DrawHeader('<IMG SRC=assets/check.gif>'.$note);
	if($_REQUEST['search_modfunc']=='list')
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=save method=POST>";
		DrawHeader('',SubmitButton(_('Add Course to Selected Students')));
		
		echo '<BR><CENTER><TABLE bgcolor=#'.Preferences('COLOR').' cellpadding=3><TR><TD align='.ALIGN_RIGHT.'>'._('Course Section(s) to Add').'</TD><TD><DIV id=course_div><UL>';
		
		echo '</UL></DIV>'."<A HREF=# onclick='window.open(\"Modules.php?modname=$_REQUEST[modname]&modfunc=choose_course\",\"\",\"scrollbars=yes,resizable=yes,width=800,height=400\");'><DIV id=choose_course_div>"._('Choose a Course')."</DIV></A></TD></TR>";
		echo '<TR><TD align='.ALIGN_RIGHT.'>'._('Start Date').'</TD><TD>'.PrepareDate(DBDate(),'').'</TD></TR>';
		
		echo '<TR><TD align='.ALIGN_RIGHT.'>'._('Marking Period').'</TD><TD>';
		echo '<SELECT name=marking_period_id><OPTION value="!">'._('Full Course Enrollment').'</OPTION><OPTION value=0>'._('Full Year').'</OPTION>';
		$semesters_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,TITLE,NULL AS SEMESTER_ID FROM SCHOOL_SEMESTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER"));
		$quarters_RET = DBGet(DBQuery("SELECT MARKING_PERIOD_ID,TITLE,SEMESTER_ID FROM SCHOOL_QUARTERS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER"));
		foreach($semesters_RET as $mp)
			echo "<OPTION value=$mp[MARKING_PERIOD_ID]>".$mp['TITLE'].'</OPTION>';
		foreach($quarters_RET as $mp)
			echo "<OPTION value=$mp[MARKING_PERIOD_ID]>".$mp['TITLE'].'</OPTION>';
		echo '</SELECT>';		
		echo '</TD></TR>';
		echo '</TABLE></CENTER><BR>';
	}
}

if(!$_REQUEST['modfunc'])
{
	if($_REQUEST['search_modfunc']!='list')
		unset($_SESSION['MassSchedule.php']);
	$extra['link'] = array('FULL_NAME'=>false);
	$extra['SELECT'] = ",CAST (NULL AS CHAR(1)) AS CHECKBOX";
	$extra['functions'] = array('CHECKBOX'=>'_makeChooseCheckbox');
	$extra['columns_before'] = array('CHECKBOX'=>'</A><INPUT type=checkbox value=Y name=controller onclick="checkAll(this.form,this.form.controller.checked,\'student\');"><A>');
	$extra['new'] = true;

	/*Widgets('course');
	Widgets('request');
	Widgets('activity');*/
	Widgets('all');
	
	Search('student_id',$extra);
	if($_REQUEST['search_modfunc']=='list')
	{
		echo '<BR><CENTER>'.SubmitButton(_('Add Course to Selected Students')).'</CENTER>';
		echo "</FORM>";
	}

}

if($_REQUEST['modfunc']=='choose_course')
{
	if($_REQUEST['course_id'])
	{
		$weights_RET = DBGet(DBQuery("SELECT COURSE_WEIGHT,GPA_MULTIPLIER FROM COURSE_WEIGHTS WHERE COURSE_ID='$_REQUEST[course_id]'"));
		if(count($weights_RET)==1)
			$_REQUEST['course_weight'] = $weights_RET[1]['COURSE_WEIGHT'];
	}

	if(!$_REQUEST['course_period_id'])
		include 'modules/Scheduling/Courses.php';
	else
	{
		$_SESSION['MassSchedule.php']['count']++;
		
		$inputs = '<INPUT type=hidden name=course_id['.$_SESSION['MassSchedule.php']['count'].'] value='.$_REQUEST['course_id'].'>';
		$inputs .= '<INPUT type=hidden name=course_weight['.$_SESSION['MassSchedule.php']['count'].'] value='.$_REQUEST['course_weight'].'>';
		$inputs .= '<INPUT type=hidden name=course_period_id['.$_SESSION['MassSchedule.php']['count'].'] value='.$_REQUEST['course_period_id'].'>';

		$course_title = DBGet(DBQuery("SELECT TITLE FROM COURSES WHERE COURSE_ID='".$_REQUEST['course_id']."'"));
		$course_title = $course_title[1]['TITLE'];
		$period_title = DBGet(DBQuery("SELECT TITLE FROM COURSE_PERIODS WHERE COURSE_PERIOD_ID='".$_REQUEST['course_period_id']."'"));
		$period_title = $period_title[1]['TITLE'];

		echo "\n<script language=javascript>opener.document.getElementById(\"course_div\").innerHTML = opener.document.getElementById(\"course_div\").innerHTML + \"<LI>$course_title - $_REQUEST[course_weight]: $period_title $inputs\";";
		if($_SESSION['MassSchedule.php']['count']==1)
			echo "\n opener.document.getElementById(\"choose_course_div\").innerHTML = \"".button('add')._('Choose another Course').'";';
		echo "\n window.close();</script>";
	}
}

function _makeChooseCheckbox($value,$title)
{	global $THIS_RET;

	return "<INPUT type=checkbox name=student[".$THIS_RET['STUDENT_ID']."] value=Y>";
}

?>